<template>
  <div id="password-modify-box">
    <el-form :model="formModel" ref="form" label-width="100px">
      <el-form-item label="账号:" prop="userId">
        <el-input v-model.number="formModel.userId"/>
      </el-form-item>
      <el-form-item label="密码:" prop="password">
        <el-input v-model="formModel.password"/>
      </el-form-item>
      <el-form-item label="用户名:" prop="useName">
        <el-input v-model="formModel.userName"/>
      </el-form-item>
      <el-form-item label="真实姓名:" prop="realName">
        <el-input v-model="formModel.realName"/>
      </el-form-item>
      <el-form-item label="角色:" prop="role">
        <el-input v-model="formModel.role" disabled/>
      </el-form-item>
      <el-form-item label="手机号:" prop="phone">
        <el-input v-model="formModel.phone"/>
      </el-form-item>
      <el-form-item label="邮箱:" prop="email">
        <el-input v-model="formModel.email"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">确认</el-button>
        <el-button type="success" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

export default {
  name: "UserAdd",
  data() {
    return {
      formModel: {
        userId: '',
        password: '',
        userName: '',
        realName: '',
        phone: '',
        email: '',
        role: this.$route.params.role,
      },
    }
  },
  computed: {},
  methods: {
    submit() {
      const url = '/users';
      const user = this.formModel;
      switch (this.formModel.role) {
        case '学生':
          user.role = 'S';
          break;
        case '教师':
          user.role = 'T';
          break;
        case '管理员':
          user.role = 'A';
          break;
        default :
          user.role = 'A';
      }
      this.axios.post(url, user).then(resp => {
          if (resp.code === 200) {
            // 修改成功
            this.$message({
              type: "success",
              message: '修改成功',
              center: true,
            });
            this.$router.go(-1);
          } else {
            this.$message({
              type: "error",
              message: resp.message,
              center: true,
            });
          }
        }
      )
    },
    cancel() {
      this.$router.go(-1);
    }
  },
}
</script>

<style scoped>

</style>